// pages/device/list/index.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js"),
formatTime = require("../../../component/common.js");
import Toast from '../../../component/vant/toast/toast';
import Dialog from '../../../component/vant/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    estates:[],
    estate_names:[],
    roomList:[],
    pageSize:10,
    pageIndex: 1,
    netWork:false,
    loading:false,
    hasData:false,
    isLoading:true,
    estate_id:0,
    showCalendar:false,
    begin_date:'',
    end_date:'',
    remark:'',
    searchValue:'',
    isShowPopupPhone:false,
    phoneList:[],
    chooseItem:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onHttpEstateList()
  },
  changeEstate(e){
    console.log('changeEstate',e);
    var estate_id = e.detail;
    this.data.estate_id = estate_id;
    this.refreshdeviceList()
  },
  onChange(val){
    console.log('onChange',val)
    this.setData({
      searchValue: val.detail
    });
  },
  onSearch(val){
    console.log('onSearch',val);
    this.data.searchValue = val.detail;
    this.refreshdeviceList()
  },
  onSearchClick(val){
    console.log('onSearchClick',val)
    this.refreshdeviceList()
  },
  goWyfList(e){
    var roomId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pagesProperty/pages/feeList/index?room_id=' + roomId,
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpEstateList: function () {
    var that = this
    fetchApi.httpPost('/estate/admin/list',{
    }, function (res) {
      console.log("获取我管辖的小区列表", res)
      if(res.data.totalcount > 0){
        var estates = res.data.data.estates;
        for (var index in estates) {
          that.data.estates.splice(that.data.estates.length, 0, estates[index]);
        }
      }
      that.data.estate_id = that.data.estates.length > 0 ?  that.data.estates[0].id : 0;
      that.setData({ 
        estates: that.data.estates,
        estate_names:res.data.data.estate_names,
        estate_id:that.data.estates.length > 0 ?  that.data.estates[0].id : 0,
      });
      
      that.onHttpInit()
      // complete
      wx.stopPullDownRefresh() //停止下拉刷新
    },function(){})
  },
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/estate/admin/room/list', {
      // begin_date:that.data.begin_date,
      // end_date:that.data.end_date,
      keyWord:that.data.searchValue,
      pageIndex:that.data.pageIndex,
      pageSize:that.data.pageSize,
      estate_id:that.data.estate_id,
    }, function (res) {
      console.log("获取房屋信息", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1
          var rooms = res.data.data.rooms;
          if(rooms.length > 0){
            console.log('rooms',rooms)
            for (var index in rooms) {
              that.data.roomList.splice(that.data.roomList.length, 0, rooms[index]);
            }
            that.setData({
              hasData: true,
              roomList: that.data.roomList
            })
          }
        }
      }else{
        Toast.fail(res.data.msg)
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true,loading: true,  isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false,loading: true,  isLoading: false, })
    })
  },
  callPhone(e){
    let that = this;
    let item = e.currentTarget.dataset.item;
    console.log('callPhone', item)
    if(item.phoneNum){
      this.data.chooseItem = item;
      let phoneList = item.phoneNumList;
      if(phoneList.length == 1){
        wx.makePhoneCall({
          phoneNumber: phoneList[0],
          success(res){
            console.log('callPhone success', res)
            that.onHttpSubmitAddRecord(item.id, phoneList[0]);
          }
        })
      }else{
        this.setData({
          isShowPopupPhone: true,
          phoneList: phoneList
        })
      }
    }else{
      Toast('暂无可联系方式');
    }
  },
  onConfirmPhone(e){
    let that = this;
    console.log('onConfirmPhone', e)
    let phone = e.detail.value;
    wx.makePhoneCall({
      phoneNumber: phone,
      success(res){
        console.log('callPhone success', res)
        that.onHttpSubmitAddRecord(that.data.chooseItem.id, phone);
      }
    })
  },
  onCancelPhone(){
    this.setData({
      isShowPopupPhone: false
    })
  },
  onHttpSubmitAddRecord: function (roomId, phoneNum) {
    var that = this;
    fetchApi.httpPost('/customer/call/record/add', {
      room_id: roomId,
      phone_num: phoneNum,
      remark: '',
    }, function (res) {
      console.log("呼叫响应", res)
      if (res.data.code == 0) {
        
      }else{
        Toast.fail(res.data.msg)
      }
    }, function () {
      that.setData({ netWork: false, hasData: false,loading: true,  isLoading: false, })
    })
  },
  goCallRecord(e){
    let that = this;
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pagesProperty/pages/call/record/index?roomId=' + item.id,
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshdeviceList()
  },
  refreshdeviceList:function(){
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      roomList:[]
    })
    this.data.pageIndex = 1
    this.onHttpInit()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoading: true
    })
    this.onHttpInit()
  },
})